//
//  WHListDetailViewController.m
//  GroupFourFM
//
//  Created by dllo on 15/12/25.
//  Copyright © 2015年 GroupFour. All rights reserved.
//

#import "WHListDetailViewController.h"
#import "WSAppTools.h"
#import "WSTrackModel.h"
#import "WSAuthorModel.h"
#import "WHListDetailTableViewCell.h"
#import "WSPlayViewController.h"
#import "WSAlbumsDetailViewController.h"
#import "WHAuthorDetailViewController.h"

@interface WHListDetailViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) WSBaseTableView *myTableView;
@property (nonatomic, strong) NSMutableArray *myArray;
@property (nonatomic, assign) NSInteger currentPage;
@property (nonatomic, assign) BOOL isUpLoading;

@end

@implementation WHListDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.myArray = [NSMutableArray array];
    self.title = self.navTitle;
    UIButton * backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 0 , 20, 30);
    backButton.backgroundColor = CLEAR;
    [backButton setImage:[UIImage imageNamed:@"btn_back_n@2x.png"] forState:UIControlStateNormal];
    [backButton setImage:[UIImage imageNamed:@"btn_back_h@2x.png"] forState:UIControlStateHighlighted];
    [backButton addTarget:self action:@selector(backButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
    //[self getData];
    [self createTableView];
    self.currentPage = 0;
    
}
#pragma mark - 返回按钮方法
-(void)backButtonAction:(UIButton *)btn
{
    [self.navigationController popViewControllerAnimated:YES];
    
}
#pragma mark - 请求数据
-(void)getData
{
    NSString *urlStr = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/discovery/v1/rankingList/%@?device=iphone&key=%@&pageId=%ld&pageSize=40", self.contentType, self.key,self.currentPage];
    
    
    [WSAppTools getDataWithURL:urlStr block:^(id result) {
        //NSLog(@"%@",result);
        
        if (self.isUpLoading == NO){
            
            [self.myArray removeAllObjects];
        }
        
        NSMutableArray *array = [result objectForKey:@"list"];
        //        NSLog(@"%@", array);
        for (NSMutableDictionary *dic in array) {
            if ([_contentType isEqualToString:@"track"]) {
                WSTrackModel *model = [WSTrackModel modelWithDictionary:dic];
                [self.myArray addObject:model];
                
            }
            else if ([_contentType isEqualToString:@"album"])
            {
                WSAlbumModel *model = [WSAlbumModel modelWithDictionary:dic];
                [self.myArray addObject:model];
            }
            else{
                
                WSAuthorModel *model = [WSAuthorModel modelWithDictionary:dic];
                [self.myArray addObject:model];
            }
        }
        //        NSLog(@"%@", self.myArray);
        [self.myTableView.mj_header endRefreshing];
        [self.myTableView.mj_footer endRefreshing];
        
        [self.myTableView reloadData];
    } error:^(NSError *error) {
        
        NSLog(@"请求数据失败  error" );

    }];
}

#pragma mark - 创建tableView
-(void)createTableView
{
    self.myTableView = [[WSBaseTableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, HEIGHT_NOBAR) style:UITableViewStylePlain];
    [self.view addSubview:self.myTableView];
    
    [self.myTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.view);
        
    }];
    
    self.myTableView.dataSource = self;
    self.myTableView.delegate = self;
    self.myTableView.rowHeight = SCREEN_HEIGHT*0.16;
    [self.myTableView registerClass:[WHListDetailTableViewCell class] forCellReuseIdentifier:@"WHListDetailTableViewCell"];
    //self.myTableView.bounces = NO;
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(downLoadMJ)];
    self.myTableView.mj_header = header;
    [self.myTableView.mj_header beginRefreshing];
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(upLoadMJ)];
    self.myTableView.mj_footer = footer;
}
#pragma mark - 刷新/加载

-(void)downLoadMJ{
    
    NSLog(@"下拉刷新");
    
    self.isUpLoading = NO;
    self.currentPage = 1;
    [self getData];
    
}

-(void)upLoadMJ
{
    NSLog(@"上拉加载");
    
    self.currentPage++;
    self.isUpLoading = YES;
    [self getData];
    
}

#pragma mark - tableView协议方法
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.myArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
     WHListDetailTableViewCell *albumCell = [tableView dequeueReusableCellWithIdentifier:@"WHListDetailTableViewCell"];
    
    if ([_contentType isEqualToString:@"track"])
    {
        WSTrackModel *model = [self.myArray objectAtIndex:indexPath.row];
        albumCell.trackModel = model;
        albumCell.numLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row+1];
        
    }
    else if ([_contentType isEqualToString:@"album"])
    {
       
        WSAlbumModel *model = [self.myArray objectAtIndex:indexPath.row];
        albumCell.albumModel = model;
        albumCell.numLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row+1];
        
        
    }
    else{
        
        WSAuthorModel *model = [self.myArray objectAtIndex:indexPath.row];
        albumCell.authorModel = model;
        albumCell.numLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row+1];
        
    }
    return albumCell;
}

#pragma mark - 点击跳转方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if ([_contentType isEqualToString:@"track"])
    {
        WSTrackModel *model = [self.myArray objectAtIndex:indexPath.row];
        WSPlayViewController *player = [WSPlayViewController sharaMusicPlayer];
        UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:player];
        player.song_id = model.trackId;
        player.allMusicArray = self.myArray;
        player.indexRow = indexPath.row;
        [self presentViewController:naVC animated:YES completion:^{
            
        }];
    }
    else if ([_contentType isEqualToString:@"album"])
    {
        WSAlbumModel *model = [self.myArray objectAtIndex:indexPath.row];
        
        WSAlbumsDetailViewController *albumsDetailVC = [[WSAlbumsDetailViewController alloc] init];
        
        albumsDetailVC.navigationController.navigationBar.translucent = YES;
        [(WSTabBarController *)self.tabBarController isHiddenTabBar:YES withViewController:albumsDetailVC];
        albumsDetailVC.albumId = model.myId;
        //        albumsDetailVC.myTitle = myTitle;
        [self.navigationController pushViewController:albumsDetailVC animated:YES];
        
    }
    else
    {
        
        WSAuthorModel *model = [self.myArray objectAtIndex:indexPath.row];
        WHAuthorDetailViewController *authorDetailVC = [[WHAuthorDetailViewController alloc] init];
        
        //UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:authorDetailVC];
        
        authorDetailVC.uid = model.uid;
        //        authorDetailVC.myTitle = myTitle;
        [self.navigationController pushViewController:authorDetailVC animated:YES];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
